//package com.dkx.springbootwebsocket.controller;
//
//import com.dkx.springbootwebsocket.service.MessageDebugService;
//import com.dkx.springbootwebsocket.service.MessageRoutingDebugService;
//import com.dkx.springbootwebsocket.service.WebSocketMessageService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.*;
//
///**
// * @Author: 杜科璇
// * @Description: TODO
// * @Date: 2025/10/31 19:59
// * @Version: 1.0
// */
//
//// DebugController.java
//@RestController
//@RequestMapping("/api/")
//@CrossOrigin(origins = "*")
//@Slf4j
//public class DebugController {
//
//    @Autowired
//    private WebSocketMessageService messageService;
//
////    @Autowired
////    private RedisTemplate<String, Object> redisTemplate;
//
//    @Autowired
//    private MessageDebugService messageDebugService;
//
//    @Autowired
//    private MessageRoutingDebugService routingDebugService;
//
////    /**
////     * 调试接口：检查离线消息状态
////     */
////    @GetMapping("/debug/offline-status/{userId}")
////    public ResponseEntity<?> checkOfflineMessageStatus(@PathVariable String userId) {
////        try {
////            Map<String, Object> result = new HashMap<>();
////
////            String key = "offline:msg:" + userId;
////            Long size = redisTemplate.opsForList().size(key);
////
////            result.put("userId", userId);
////            result.put("offlineMessageCount", size != null ? size : 0);
////            result.put("hasOfflineMessages", size != null && size > 0);
////
////            // 检查用户在线状态
////            boolean isOnline = messageService.isUserOnline(userId);
////            result.put("userOnline", isOnline);
////
////            // 检查用户订阅
////            Map<String, Object> subscriptionStatus = messageService.debugCheckUserSubscriptions(userId);
////            result.put("subscriptionStatus", subscriptionStatus);
////
////            return ResponseEntity.ok(result);
////        } catch (Exception e) {
////            log.error("检查离线消息状态失败", e);
////            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("检查离线消息状态失败");
////        }
////    }
//
////    @GetMapping("/sessions")
////    public ResponseEntity<Map<String, Object>> getActiveSessions() {
////        Map<String, Object> result = new HashMap<>();
////
////        // 这里可以添加获取活跃会话的逻辑
////        // 注意：这需要根据你的具体实现来获取活跃会话
////
////        result.put("timestamp", System.currentTimeMillis());
////        result.put("message", "调试接口 - 需要实现具体的会话获取逻辑");
////
////        return ResponseEntity.ok(result);
////    }
//
////    @GetMapping("/redis/keys")
////    public ResponseEntity<List<String>> getRedisKeys() {
////        List<String> keys = new ArrayList<>();
////
////        // 获取所有与用户会话相关的Redis键
////        Set<String> patternKeys = messageService.getRedisTemplate().keys("user:session:*");
////        if (patternKeys != null) {
////            keys.addAll(patternKeys);
////        }
////
////        // 获取所有离线消息相关的Redis键
////        Set<String> offlineKeys = messageService.getRedisTemplate().keys("offline:msg:*");
////        if (offlineKeys != null) {
////            keys.addAll(offlineKeys);
////        }
////
////        return ResponseEntity.ok(keys);
////    }
////
////    @GetMapping("/redis/value/{key}")
////    public ResponseEntity<Object> getRedisValue(@PathVariable String key) {
////        Object value = messageService.getRedisTemplate().opsForValue().get(key);
////        return ResponseEntity.ok(value);
////    }
////    @PostMapping("/send-test")
////    public ResponseEntity<Map<String, Object>> sendTestMessage(
////            @RequestParam String fromUser,
////            @RequestParam String toUser,
////            @RequestParam String content) {
////        Map<String, Object> result = new HashMap<>();
////
////        try {
////            messageDebugService.sendTestMessage(fromUser, toUser, content);
////            result.put("success", true);
////            result.put("message", "测试消息已发送");
////            return ResponseEntity.ok(result);
////        } catch (Exception e) {
////            log.error("发送测试消息失败", e);
////            result.put("success", false);
////            result.put("message", "发送失败: " + e.getMessage());
////            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
////        }
////    }
//
////    @GetMapping("/user-status/{userId}")
////    public ResponseEntity<Map<String, Object>> getUserStatus(@PathVariable String userId) {
////        Map<String, Object> result = new HashMap<>();
////
////        try {
////            boolean isOnline = messageService.isUserOnline(userId);
////            result.put("userId", userId);
////            result.put("online", isOnline);
////            result.put("timestamp", System.currentTimeMillis());
////            result.put("success", true);
////            return ResponseEntity.ok(result);
////        } catch (Exception e) {
////            log.error("获取用户状态失败", e);
////            result.put("success", false);
////            result.put("message", "获取失败: " + e.getMessage());
////            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
////        }
////    }
//
//    @GetMapping("/all-users-status")
//    public ResponseEntity<Map<String, Object>> getAllUsersStatus() {
//        Map<String, Object> result = new HashMap<>();
//
//        try {
//            // 检查一些测试用户的状态
//            String[] testUsers = {"1", "2", "3", "4"};
//            Map<String, Boolean> userStatus = new HashMap<>();
//
//            for (String user : testUsers) {
//                userStatus.put(user, messageService.isUserOnline(user));
//            }
//
//            result.put("userStatus", userStatus);
//            result.put("timestamp", System.currentTimeMillis());
//            result.put("success", true);
//            return ResponseEntity.ok(result);
//        } catch (Exception e) {
//            log.error("获取所有用户状态失败", e);
//            result.put("success", false);
//            result.put("message", "获取失败: " + e.getMessage());
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
//        }
//    }
////    @PostMapping("/debug-routing")
////    public ResponseEntity<Map<String, Object>> debugMessageRouting(
////            @RequestParam String fromUser,
////            @RequestParam String toUser,
////            @RequestParam String content) {
////        Map<String, Object> result = new HashMap<>();
////
////        try {
////            routingDebugService.debugMessageRouting(fromUser, toUser, content);
////            result.put("success", true);
////            result.put("message", "消息路由调试完成");
////            return ResponseEntity.ok(result);
////        } catch (Exception e) {
////            log.error("消息路由调试失败", e);
////            result.put("success", false);
////            result.put("message", "调试失败: " + e.getMessage());
////            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
////        }
////    }
////
////
////    @GetMapping("/subscriptions")
////    public ResponseEntity<Map<String, Object>> getSubscriptions() {
////        Map<String, Object> result = new HashMap<>();
////
////        try {
////            // 获取所有活跃订阅
////            Map<String, List<String>> userSubscriptions = new HashMap<>();
////
////            // 这里需要根据你的实现来获取订阅信息
////            // 如果是使用 SimpUserRegistry，可以这样获取：
////            // for (SimpUser user : simpUserRegistry.getUsers()) {
////            //     List<String> subscriptions = new ArrayList<>();
////            //     for (SimpSession session : user.getSessions()) {
////            //         for (SimpSubscription subscription : session.getSubscriptions()) {
////            //             subscriptions.add(subscription.getDestination());
////            //         }
////            //     }
////            //     userSubscriptions.put(user.getName(), subscriptions);
////            // }
////
////            result.put("subscriptions", userSubscriptions);
////            result.put("timestamp", System.currentTimeMillis());
////            result.put("success", true);
////            return ResponseEntity.ok(result);
////        } catch (Exception e) {
////            log.error("获取订阅信息失败", e);
////            result.put("success", false);
////            result.put("message", "获取失败: " + e.getMessage());
////            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
////        }
////    }
//}